<?php 
namespace Application\Navigation;

use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;

class Navigation extends DefaultNavigationFactory
{
	protected function getPages(ServiceLocatorInterface $serviceLocator)
	{
		if (null === $this->pages) {
			//FETCH data from table menu :
			$fetchMainMenu = $serviceLocator->get('menu')->fetchAll();

			$configuration['navigation'][$this->getName()] = array();
		  foreach($fetchMainMenu as $MainMenuItem=>$MainMenuField)
		 	{

				$fetchSubMenu = $serviceLocator->get('menu')->fetchAllSubMenus($MainMenuField['ID_M']);
				$subMenu = array();
				foreach($fetchSubMenu as $SubMenuItem=>$SubMenuField)
				{
					array_push($subMenu,
					array(
					'label' => $SubMenuField['LABEL_M'],
					'route' => $SubMenuField['ROUTE_M'],
					'idsub' => $SubMenuField['ID_SUB_M'],
					'active' => false,
					)
					);
			}

			$configuration['navigation'][$this->getName()][$MainMenuField['ID_M']] = array(
					'label' => $MainMenuField['LABEL_M'],
					'route' => $MainMenuField['ROUTE_M'],
					'pages' => $subMenu,
					'issub' => $MainMenuField['ISSUB_M'],
					'idm' => $MainMenuField['ID_M'],
					'active' => false,
			);
			}

			if (!isset($configuration['navigation'])) {
			throw new Exception\InvalidArgumentException('Could not find navigation configuration key');
			}
			if (!isset($configuration['navigation'][$this->getName()])) {
			throw new Exception\InvalidArgumentException(sprintf(
			'Failed to find a navigation container by the name “%s”',
			$this->getName()
			));
	}

	$application = $serviceLocator->get('Application');
	$routeMatch = $application->getMvcEvent()->getRouteMatch();
	$router = $application->getMvcEvent()->getRouter();
	$pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);

$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}
} 
/* namespace Application\Navigation;

use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Navigation\Service\DefaultNavigationFactory;
//use Application\Model\MenuTable;
 class Navigation extends DefaultNavigationFactory
{
protected function getPages(ServiceLocatorInterface $serviceLocator){
      if (null === $this->pages) {
         //lấy dữ liệu từ bảng menu menu :
         //var_dump('ok');
         $fetchMenu = $serviceLocator->get('albumss')->fetchAll();
        // $fetchMenu = array('das','gfer');
         $configuration['navigation'][$this->getName()] = array();
         // gán dữ liệu vào configuration['navigation']
         foreach($fetchMenu as $key=>$row){
         	//$fetchsubMenu = $serviceLocator->get('albumss')->fetchAllSubMenus($row['id']);
            $configuration['navigation'][$this->getName()][$row['title']] = array(
                  'label' => $row['label'],
                  'route' => $row['route'],
            	  
            );
         }

         $application = $serviceLocator->get('Application');// Lấy các thông tin đăng ký service
         $routeMatch  = $application->getMvcEvent()->getRouteMatch(); // Lấy ra các thông số route
         $router      = $application->getMvcEvent()->getRouter(); // lấy ra các giá trị trên url
         $pages       = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]); // lấy ra mảng config của Zend\Config\Config

         $this->pages = $this->injectComponents($pages, $routeMatch, $router);
      }
      return $this->pages;
   }
} */
